//
//  GCMAboutTaskViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/3/22.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "GCMAboutTaskViewController.h"
#import "GCMAddTaskViewController.h"
#import "GCMAboutTaskTableViewCell.h"
#import "GCMAboutTaskModel.h"
#import "GCMTaskDetailViewController.h"

@interface GCMAboutTaskViewController ()<UITableViewDataSource, UITableViewDelegate,UIAlertViewDelegate>{
    
    NSMutableArray *_DataArray;
    UITableView *_tableView;
    NSString *_returnMsg;
    NSMutableArray *_yearArr;
    NSMutableArray *_monthArr;
    NSMutableArray *_dataSource;
    NSInteger _currentPage;
}

@end

@implementation GCMAboutTaskViewController

-(void)viewWillAppear:(BOOL)animated{


}
-(void)viewDidAppear:(BOOL)animated{

    [self setNavigationBar];
    [self initData];
    [self downLoadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentPage = 1;
    
    [self createTableView];
    //    下拉刷新
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        // 进入刷新状态后会自动调用这个block
        [self downLoadData];
        [_tableView.mj_header endRefreshing];
        
    }];
    //    上拉加载
    _tableView.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 进入加载状态后会自动调用这个block
        _currentPage ++;
        NSString *pageStr  = [NSString stringWithFormat:@"%ld",_currentPage];
        
        [self downloadNextPageTableViewDataWith:pageStr];
        [_tableView.mj_footer endRefreshing];
    }];
}


-(void)createTableView{
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, SCREEN_WIDTH, SCREEN_HEIGHT )];
    _tableView.backgroundColor  = [UIColor redColor];
    _tableView.delegate = self;
    _tableView.dataSource =self;
    _tableView.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_tableView];
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMAboutTaskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[GCMAboutTaskTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor colorWithRed:.93 green:.93 blue:.93 alpha:1]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //    cell.textLabel.textColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1];
    GCMAboutTaskModel *aboutModel = _dataSource[indexPath.row];
   // NSLog(@"%@",aboutModel.contact);
    [cell setAboutModel:aboutModel];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 180;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCMTaskDetailViewController * taskVC = [[GCMTaskDetailViewController alloc]init];
    GCMAboutTaskModel *model = _dataSource[indexPath.row];
    NSLog(@"%@",model);
    taskVC.idd = model.idd;
    [self.navigationController pushViewController:taskVC animated:YES ];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"任务详情";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
}


//初始化数据

-(void)initData{
    
    _dataSource = [[NSMutableArray alloc]init];
}

-(void)downLoadData{
    
    //    请求接待的分类数据
    [GCMTaskHttpRequst postDataWithreceiveId:self.idd withPage:@"1" Success:^(id obj, NSError *err) {
        [_dataSource removeAllObjects];
         _dataSource = obj;
        //NSLog(@"data****%@",obj);
        [_tableView reloadData];
    }];
}
-(void)downloadNextPageTableViewDataWith:(NSString*)pageStr{
    //    改变当前页
    //_currentPage ++;
    [GCMTaskHttpRequst postDataWithreceiveId:self.idd withPage:pageStr Success:^(id obj, NSError *err) {
        [_dataSource addObjectsFromArray:obj];
        [_tableView reloadData];
    }];
}

//设置navigationBar
-(void)setNavigationBar{
    
    self.navigationController.navigationBarHidden = NO;
    
    self.navigationController.navigationBar.barTintColor =[UIColor colorWithRed:0.12 green:0.34 blue:0.57 alpha:1.0];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setTitle:@"新增" forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(GotoSettings)
          forControlEvents:UIControlEventTouchUpInside];
    rightButton.frame = CGRectMake(0, 2,40, 40);
    UIBarButtonItem *menuRButton = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = menuRButton;
    
}
-(void)GotoSettings{
    GCMAddTaskViewController * taskVC = [[GCMAddTaskViewController alloc]init];
    [self.navigationController pushViewController:taskVC animated:YES ];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    taskVC.idd= self.idd;
    taskVC.taskAboutArray = self.taskAboutArray;
    backItem.title = @"新增任务";
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem = backItem;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
